package com.csun.cmny.provider.controller;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.OmcPaymentCycle;
import com.csun.cmny.provider.model.dto.PaymentCycleDto;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcPaymentCycleService;
import com.csun.cmny.provider.validator.PaymentCycleSaveValidator;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping(value = "/paymentCycle", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcPaymentCycleController extends BaseController {

    @Resource
    private OmcPaymentCycleService omcPaymentCycleService;
    @Resource
    private CmnyMessageSource cmnyMessageSource;

    /**
     * 保存
     * @param: [paymentCycleDto]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
    //@LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/save")
    public Wrapper savePaymentCycle(@Validated(PaymentCycleSaveValidator.class) @RequestBody PaymentCycleDto paymentCycleDto, BindingResult br) {

        int result = omcPaymentCycleService.savePaymentCycle(convert(paymentCycleDto),getLoginAuthDto());

        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), result);
    }

    /**
     * 查询
     * @param: [groupId]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
    //@LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/getByGroupId/{groupId}")
    public Wrapper saveDaysTimeConfig(@PathVariable Long groupId) {

        OmcPaymentCycle omcPaymentCycle = new OmcPaymentCycle();
        omcPaymentCycle.setGroupId(groupId);
        omcPaymentCycle = omcPaymentCycleService.selectOne(omcPaymentCycle);
        //return WrapMapper.ok(omcPaymentCycle);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), omcPaymentCycle);
    }

    /**
     * 转化
     * @param: [paymentCycleDto]
     * @return: com.csun.cmny.provider.model.domain.OmcPaymentCycle
     */
    public OmcPaymentCycle convert(PaymentCycleDto paymentCycleDto){
        OmcPaymentCycle omcPaymentCycle = new OmcPaymentCycle();
        BeanUtils.copyProperties(paymentCycleDto,omcPaymentCycle);
        return omcPaymentCycle;
    }
}
